/**
 * Copyright (C) 2016-2019 DSpot Sp. z o.o
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dspot.declex.override.handler;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;

import com.dspot.declex.helper.ActionHelper;
import com.dspot.declex.holder.EventHolder;

import org.ohosannotations.OhosAnnotationsEnvironment;
import org.ohosannotations.ElementValidation;
import org.ohosannotations.holder.EFractionHolder;

import com.dspot.declex.override.holder.FragmentActionHolder;


/**
 * efraction处理程序
 *
 * @author Administrator
 * @date 2021/07/27
 */
public class EFractionHandler extends org.ohosannotations.internal.core.handler.EFractionHandler {

    /**
     * efraction处理程序
     *
     * @param environment 环境
     */
    public EFractionHandler(OhosAnnotationsEnvironment environment) {
        super(environment);
    }

    /**
     * 验证
     *
     * @param element 元素
     * @param valid 有效的
     */
    @Override
    public void validate(Element element, ElementValidation valid) {
        if (element.getKind().equals(ElementKind.CLASS)) {
            ActionHelper.getInstance(getEnvironment()).validate(element, this);
        }

        super.validate(element, valid);

        if (!getEnvironment().getValidatedElements().isAncestor(element)) {
            FragmentActionHolder.createInformationForActionHolder(element, getEnvironment());
        }
    }

    /**
     * 过程
     *
     * @param element 元素
     * @param holder 持有人
     */
    @Override
    public void process(Element element, EFractionHolder holder) {
        super.process(element, holder);

        if (!getEnvironment().getValidatedElements().isAncestor(element)) {
            FragmentActionHolder actionHolder = holder.getPluginHolder(new FragmentActionHolder(holder));
            actionHolder.getFragmentAction();
        }

        EventHolder eventHolder = holder.getPluginHolder(new EventHolder(holder));
        eventHolder.setEventRegisteringBlock(holder.getOnResumeAfterSuperBlock());
        eventHolder.setEventUnregisteringBlock(holder.getOnPauseBeforeSuperBlock());
    }

}
